<template>
  <div class="header">
    <div class="header-left">
      <p>叩丁狼电商运营平台</p>
      <el-icon @click="$emit('changeIsCollapseFn', !isCollapse)">
        <Menu />
      </el-icon>
    </div>
    <el-dropdown @command="commandHandle">
      <span class="el-dropdown-link">
        {{ userStore.username }}
        <el-icon>
          <arrow-down />
        </el-icon>
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="index">首页</el-dropdown-item>
          <el-dropdown-item command="logout">退出</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script setup lang="ts">
import { ArrowDown, Menu } from "@element-plus/icons-vue";
import { useUserStore } from "@/store/user";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { logout } from "@/common";
const { isCollapse } = defineProps(["isCollapse"]);
const userStore = useUserStore();

const router = useRouter();

const commandHandle = (command: string) => {
  if (command === "index") {
    router.push("/index");
  } else if (command == "logout") {
    // 退出要删token、全局数据pinia、动态路由、跳转首页
    logout(
      () => {},
      () => {
        ElMessage.success("退出成功");
      }
    );

    router.push("/login");
  }
};
</script>

<style scoped lang="less">
.header {
  height: 100%;
  display: flex;
  align-items: center;
  justify-items: center;
  justify-content: space-between;

  .header-left {
    font-size: 24px;
    font-weight: 400;
    color: #fff;
    display: flex;
    align-items: center;
    justify-items: center;

    :deep(.el-icon) {
      margin: 5px 0 0 10px;
    }
  }

  :deep(.el-dropdown-link) {
    color: #fff;
  }
}
</style>
